import { ref, reactive, onMounted, toRaw, UnwrapRef } from "vue";
import dayjs, { Dayjs } from "dayjs";
import GLOBAL_SETTING from './Constant'
import Utils from "@/utils/Utils";
import _ from "lodash";
import ThdFramesetExampleAPI from "@/api/ThdFramesetExampleAPI";

export default function useHooks() {

  // loading
  let loading = ref<boolean>(false);

  // 数据列表
  let tableData = ref([]);
  // 分页数据
  let pagination = ref(_.cloneDeep(GLOBAL_SETTING.pagination));

  // 查询数据
  const getList = (clearPage: boolean) => {
    const queryCondition = toRaw(formState.value);
    Utils.dateToStr(queryCondition, "birthday");
    Utils.dateToStr(queryCondition, "createTime");
    Utils.dateToStr(queryCondition, "modifyTime");

    const sortInfo = toRaw(sort.value);
    if (sortInfo.sortField) {
      queryCondition.sortField = sortInfo.sortField;
      queryCondition.sortOrder = sortInfo.sortOrder;
    }
    console.log(queryCondition);

    loading.value = true;
    // debugger;
    if (clearPage) {
      pagination.value.current = 1;
    }

    ThdFramesetExampleAPI.findList({
      pageSize: pagination.value.pageSize,
      pageNum: pagination.value.current,
      ...queryCondition,
    })
      .then((res) => {
        // debugger;
        console.log(res);
        if (res.code === "0") {
          tableData.value = res.result.list;
          pagination.value.current = res.result.pageNum;
          pagination.value.total = res.result.total;
          pagination.value.pageSize = res.result.pageSize;
        }
        loading.value = false;
      })
      .catch(function (error) {
        console.log(error);
        loading.value = false;
      });
  };


  // 
  onMounted(function () {
    getList();
  });


  // 改变分页信息
  function paginationChange() {
    // console.log(pagination);
    getList(false);
  }



  // ==================== 删除数据 ====================
  function deleteById(id: string) {
    ThdFramesetExampleAPI.deleteThdFramesetExampleById(id)
      .then((res: any) => {
        if (res.code === "0") {
          hooks.loading.value = false;
          hooks.getList(false);
        } else {
          console.log("error");
          hooks.loading.value = false;
        }
      })
      .catch(function (error) {
        console.log(error);
        hooks.loading.value = false;
      });
  }



  interface FormState {
    userName: string;
    age: number;
    birthday: [];
    createTime: [];
    modifyTime: [];
  }

  const blankFormData = {
    userName: "",
    ageFrom: null,
    ageTo: null,
    birthday: [],
    createTime: [],
    modifyTime: [],
  };

  // 搜索条件
  let formState: UnwrapRef<FormState> = ref(_.cloneDeep(blankFormData));

  // 重置搜索条件
  function resetFrom() {
    formState.value.userName = "";
    formState.value.ageFrom = null;
    formState.value.ageTo = null;
    formState.value.birthday = [];
    formState.value.createTime = [];
    formState.value.modifyTime = [];
    getList(true);
  }




  // ==================== 排序 ===========================
  let sort = ref({
    sortOrder: "",
    sortField: "",
  });
  function sortChange(pagination, filters, sorter) {
    console.log(sorter);
    sort.value = {
      sortOrder: sorter.order === "ascend" ? "asc" : "desc",
      sortField: sorter.field,
    };
    getList();
  }


  return {
    loading, // loading 状态
    tableData, // 表格数据
    pagination, // 分页信息
    sort, // 排序信息
    formState, // 搜索条件

    sortChange, // 排序改变
    getList, // 查询数据
    resetFrom, // 重置表单
    paginationChange, // 修改分页信息
    deleteById // 根据id删除数据
  }
}